import requests
import base64
import urllib


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import json

import cv2

# #获取token

# res = requests.get('https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=7BzC4ag74TgA2BSGCGDV9445&client_secret=E31EUMUyPHGv4ViWP21tq4ODCfEHLqib')

# access_token = res.json()['access_token']

# #开始智能视图

# #接口地址
# url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token='+access_token

# #定义头部信息
# myheaders = {'Content-Type':'application/x-www-form-urlencoded'}

# #操作图片
# #读取图片
# # myimg = open(r'C:\Users\13935\Desktop\zuoye\students\mydjango\myapp\code.png','rb')
# myimg = open('./code1.png','rb')
# temp_img = myimg.read()
# myimg.close()

# #进行base64编码
# temp_data = {'image':base64.b64encode(temp_img)}

# #对图片地址进行
# temp_data = urllib.parse.urlencode(temp_data)

# #请求识图接口
# res = requests.post(url=url,data=temp_data,headers=myheaders)

# code = res.json()['words_result'][0]['words']
# code = str(code).replace(' ','')


# print(code)










#建立浏览器对象
browser = webdriver.Chrome()

#打开网址
browser.get('http://localhost:8080/login')
time.sleep(1)

#选择元素
myimg = browser.find_element_by_xpath('/html/body/div[1]/div/section/div/div[2]/table/tr[4]/td[2]/img')


#截取元素图
myimg.screenshot('login.png')


#读图
imgs = cv2.imread('./login.png',cv2.IMREAD_GRAYSCALE)

print(imgs)

#写图
cv2.imwrite('./login1.png',imgs)


#selenium 截图
# browser.get_screenshot_as_file('register.png')

#请求百度接口
res = requests.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=7BzC4ag74TgA2BSGCGDV9445&client_secret=E31EUMUyPHGv4ViWP21tq4ODCfEHLqib")

#转码
res = json.loads(str(res.text))
token = res['access_token']

#识别图像
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + token

#构造头部
header = {'Content-Type':'application/x-www-form-urlencoded'}

#构造图片
img = open('./login1.png','rb')
temp_img = img.read()







#构建参数
data = {'image':base64.b64encode(temp_img)}

#编码
data = urllib.parse.urlencode(data)

#发送请求
res = requests.post(url=url,data=data,headers=header)


#将json数据转为python数据格式
code = json.loads(res.text)



#获取验证码
code = code['words_result'][0]['words']
print(code)
# 填入用户信息
browser.find_element_by_xpath('/html/body/div[1]/div/section/div/div[2]/table/tr[1]/td[2]/input').send_keys('123')

browser.find_element_by_xpath('/html/body/div[1]/div/section/div/div[2]/table/tr[2]/td[2]/input').send_keys('123')

#填入验证码
browser.find_element_by_xpath('/html/body/div[1]/div/section/div/div[2]/table/tr[3]/td[2]/input').send_keys(code.strip().replace(' ',''))


#定位滑块选择器
button = browser.find_element_by_class_name('dv_handler')
#建立动作对象
action = ActionChains(browser)
#按住拖动
action.click_and_hold(button).perform()
#动作释放
action.reset_actions()
#获取总长度
mytest = browser.find_element_by_class_name('dv_text')
print(mytest.size.get('width'))
#获取滑块长度
print(button.size.get('width'))
#拖动位置
action.move_by_offset(mytest.size.get('width')-button.size.get('width'),0).perform()





#登录按钮
browser.find_element_by_xpath('/html/body/div[1]/div/section/div/div[2]/table/tr[6]/td[2]/button/span').click()

time.sleep(3)

browser.close()

